/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Bank;

/**
 *
 * @author khanhanh
 */
public class BankDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;
    private ResultSet rs;

    public Bank getBankDetailsByCusID(int customerID){
        Bank bank = null;
        try {
            cs = conn.createConnection().prepareCall("{call sp_GetBankByCusID(?)}");
            cs.setInt("CustomerID", customerID);

            rs = cs.executeQuery();
            while (rs.next()) {
                bank = new Bank();
                bank.setCustomerID(rs.getInt("CustomerID"));
                bank.setCreditCardNumber(rs.getString("CreditCardNumber"));
                bank.setPinCode(rs.getString("PINCode"));
                bank.setBalance(rs.getFloat("Balance"));
            }
            return bank;
        } catch (SQLException ex) {
            Logger.getLogger(BankDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bank;
    }

    public Bank getBankDetails(String ccNumber, String pinCode) {
        Bank bank = null;
        try {
            cs = conn.createConnection().prepareCall("{call sp_GetBank(?,?)}");
            cs.setString("CCNumber", ccNumber);
            cs.setString("PINCode", pinCode);

            rs = cs.executeQuery();
            while (rs.next()) {
                bank = new Bank();
                bank.setCustomerID(rs.getInt("CustomerID"));
                bank.setCreditCardNumber(rs.getString("CreditCardNumber"));
                bank.setPinCode(rs.getString("PINCode"));
                bank.setBalance(rs.getFloat("Balance"));
            }
            return bank;
        } catch (SQLException ex) {
            Logger.getLogger(BankDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bank;
    }

    public boolean deposit(String ccNumber, String pinCode, float amount) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_Deposit(?,?,?)}");
            cs.setString("CCNumber", ccNumber);
            cs.setString("PINCode", pinCode);
            cs.setFloat("Amount", amount);

            if (cs.executeUpdate()==1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(BankDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean withdraw(String ccNumber, String pinCode, float amount){
        try {
            cs = conn.createConnection().prepareCall("{call sp_Withdraw(?,?,?)}");
            cs.setString("CCNumber", ccNumber);
            cs.setString("PINCode", pinCode);
            cs.setFloat("Amount", amount);

            if (cs.executeUpdate()==1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(BankDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
